// pages/order/order.js
const app=getApp();
Page({

  /*** 页面的初始数据*/
  data: {
    directory: app.globalData.directory,
    nav: [{ txt: '点菜'}, { txt: '评价'}],
    current:0,
    foods:[],
    foodspec:{},
    condcrcurrent:0,
    numzong:0,
    priceszong:0,
    shopspec: true,
    goodindex:0,
    specac: [],
    specidac:[],
    foodid:'',//商品ID
    pj:[],
    // pjt: ['全部(320)', '追评(52)', '服务好(52)','快递好(56)','差评(10)'],
    pjindex:0,
    shopcar:[],
    addnum:true,
  },
  navbind:function(e){
    var nav = this.data.nav;
    this.setData({
      current: e.currentTarget.id
    })
  },
  conswiper:function(e){
    var nav = this.data.nav;
    this.setData({
      current: e.detail.current 
    })
  },
  condcrtext:function(e){
    this.setData({
      condcrcurrent: e.currentTarget.dataset.id
    })
  },
  /*单纯加入购物车*/
  shopcaradd:function(e){
    var that=this;
    var shopcar = that.data.shopcar;
    var bol=true;
    var foodid = that.data.foods[that.data.condcrcurrent].id;
    var goodid = that.data.foods[that.data.condcrcurrent].goods[e.currentTarget.id].id;
    if(shopcar.length==0){
      shopcar.push({
        foodid: foodid,
        foodindex: that.data.condcrcurrent,
        goodid: goodid,
        goodindex: e.currentTarget.id,
        selected: false,
        price: that.data.foods[that.data.condcrcurrent].goods[e.currentTarget.id].sale_price,
        type:0,
        num:0
      })
    }
    shopcar.forEach(function(item,index){
      if (item.foodid == foodid && item.goodid == goodid){
          item.num=++item.num;
          bol=false;
      }
    })
    if(bol){
      shopcar.push({
        foodid: foodid,
        foodindex: that.data.condcrcurrent,
        goodid: goodid,
        goodindex: e.currentTarget.id,
        selected: false,
        price: that.data.foods[that.data.condcrcurrent].goods[e.currentTarget.id].sale_price,
        type: 0,
        num: 1
      })
    }
    that.setData({
      shopcar:shopcar
    })
    this.znumprice();
    wx.setStorageSync('shopcar', that.data.shopcar);
},
  /*格式中的加入购物车 */
  shopcaradd2: function (e) {
    var that = this;
    var shopcar = that.data.shopcar;
    var bol = true;
    var foodid = that.data.foods[that.data.condcrcurrent].id;
    var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    if (shopcar.length == 0) {
      shopcar.push({
        foodid: foodid,
        foodindex: that.data.condcrcurrent,
        goodid: goodid,
        goodindex: that.data.goodindex,
        price: e.currentTarget.dataset.pri,
        selected:false,
        type: 1,
        num: 0,
        specidac:that.data.specidac,
        specac: that.data.specac,
      })
    }
    shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        if(item.type==1&&item.specidac.toString()==that.data.specidac.toString()){
          item.num = ++item.num;
          bol = false;
        }
      }
    })
    if (bol) {
      shopcar.push({
        foodid: foodid,
        foodindex: that.data.condcrcurrent,
        goodid: goodid,
        goodindex: that.data.goodindex,
        price: e.currentTarget.dataset.pri,
        selected: false,
        type: 1,
        num: 1,
        specidac: that.data.specidac,
        specac: that.data.specac,
      })
    }
    that.setData({
      shopcar: shopcar,
      addnum:false
    })
    this.znumprice();
    wx.setStorageSync('shopcar', that.data.shopcar);
  },
  shopspechide:function(e){
    this.setData({
      shopspec:true
    })
  },
  /*显示格式*/
  shopspecshow:function(e){
    var specidac = [];
    var specac=[];
    var that=this;
    var bol = true;
    var foodid = that.data.foods[that.data.condcrcurrent].id;
    var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    that.data.foodid = e.currentTarget.dataset.id,
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsSpec/'+e.currentTarget.dataset.id,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          var spec = res.data.data.model.spec;
          spec.forEach(function (items, index) {
            specac[index] = items.item[0].name,
            specidac[index] = items.item[0].id
          })

          that.data.shopcar.forEach(function (item, index) {
            if (item.foodid == foodid && item.goodid == goodid) {
              if (item.type == 1 && item.specidac.toString() == specidac.toString()) {
                bol = false;
                specac = item.specac;
                specidac = item.specidac;
              }
            }
          })
          if (!bol) {
            that.setData({
              addnum: false
            })
          } else {
            that.setData({
              addnum: true
            })
          }
          that.setData({
            shopspec: false,
            foodid:that.data.foodid,
            goodindex: e.currentTarget.id,
            specac: specac,
            specidac: specidac,
            foodspec: res.data.data,
            'foodspec.tit':e.currentTarget.dataset.tit
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },

  /*格式中价格的变化*/
  speccon:function(e){
    var specidacstr='';
    var that=this;
    var bol=true;
    var foodid = that.data.foods[that.data.condcrcurrent].id;
    var goodid = that.data.foods[that.data.condcrcurrent].goods[that.data.goodindex].id;
    this.data.specac[e.currentTarget.dataset.id] = e.currentTarget.dataset.text;
    this.data.specidac[e.currentTarget.dataset.id] = e.currentTarget.dataset.current;
    this.setData({
      specac: that.data.specac,
      specidac:that.data.specidac
    })
    that.data.shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        if (item.type == 1 && item.specidac.toString() == that.data.specidac.toString()) {
          bol = false;
        }
      }
    })
    if(!bol){
      that.setData({
        addnum:false
      })
    }else{
      that.setData({
        addnum: true
      })
    }
    that.data.specidac.forEach(function(item,index){
      specidacstr+='_'+item;
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsSpecPrice/',
      data:{
        'id':that.data.foodid,
        'spec_ids': specidacstr,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 'ok') {
          that.setData({
            'foodspec.price.price':res.data.data
          })

        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  shopspechidel: function (e) {
  },
  pjt:function(e){
    this.setData({
      pjindex:e.currentTarget.id
    })
  },
  /*总的价格数量 */
  znumprice: function () {
    var znum = 0;
    var zprice = 0;
    if (typeof (this.data.shopcar)=='string') {
      this.setData({
        shopcar:[],
      })
      return false;      
    }
    this.data.shopcar.forEach(function (item, index) {
      znum += item.num
      zprice += item.price * item.num
    })
    if(znum>99){
      znum='99+'
    }
    zprice=parseInt(zprice*100)/100;
    this.setData({
      numzong: znum,
      priceszong: zprice,
    })
  },
  /*预览照片 */
  previewImage:function(e){
    console.log(e.currentTarget.dataset.img, e.currentTarget.dataset.imgs);
    var imgs = e.currentTarget.dataset.imgs;
    var imgs1 = [];
    imgs.forEach(function(item,index){
      imgs1.push(item.img)
    })
    wx.previewImage({
      current:e.currentTarget.dataset.img,
      urls:imgs1
    })
  },
  /*** 生命周期函数--监听页面加载*/
  
  onLoad: function (options) {
    if(options.id){
      app.globalData.tableid = options.id;
    }
    var that=this; 
    that.setData({
      foods: app.globalData.foodall,
      shopcar: wx.getStorageSync('shopcar')
    })
    /*评论*/
    wx.request({
      url: app.globalData.directory + 'api/v2/comment/getlist',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            pj: res.data.data
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    
    this.znumprice();
  },

  /*** 生命周期函数--监听页面初次渲染完成*/
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      shopcar: wx.getStorageSync('shopcar')
    })
    this.znumprice();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})